package com.zb.kc.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zb.kc.commonutils.R;
import com.zb.kc.entity.EduCourse;
import com.zb.kc.entity.EduCourseDescription;
import com.zb.kc.entity.vo.CourseInfoVo;
import com.zb.kc.entity.vo.CoursePublishVo;
import com.zb.kc.service.IEduCourseDescriptionService;
import com.zb.kc.service.IEduCourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author wanggen
 * @since 2020-04-10
 */
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private IEduCourseService courseService;

    @Autowired
    private IEduCourseDescriptionService descriptionService;

    @ApiOperation(value = "添加课程基本信息")
    @PostMapping
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String cid = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId", cid);
    }

    @ApiOperation(value = "根据课程id查询课程基本信息")
    @GetMapping("{id}")
    public R getCourseInfoById(@PathVariable("id") String id) {
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        // 根据id查询课程基本信息
        EduCourse course = courseService.getById(id);
        // 给courseInfo赋值课程基本信息
        BeanUtils.copyProperties(course, courseInfoVo);
        // 查询课程描述信息
        EduCourseDescription description = descriptionService.getById(id);
        // 赋值课程简介
        if (ObjectUtil.isNotNull(description)) {
            BeanUtils.copyProperties(description, courseInfoVo);
        }
        return R.ok().data("courseInfo", courseInfoVo);
    }

    @ApiOperation(value = "修改课程基本信息")
    @PutMapping
    public R updateCourseInfoById(@RequestBody CourseInfoVo courseInfoVo) {
        EduCourse course = new EduCourse();
        // 初始化course
        BeanUtils.copyProperties(courseInfoVo, course);
        courseService.updateById(course);
        EduCourseDescription description = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo, description);
        descriptionService.updateById(description);
        return R.ok().message("修改成功");
    }

    @ApiOperation(value = "根据课程id获取课程的发布信息")
    @GetMapping("getCoursePublishInfo/{courseId}")
    public R getCoursePublishInfo(@PathVariable("courseId") String courseId) {
        CoursePublishVo coursePublishVo = courseService.getCoursePublishById(courseId);
        return R.ok().data("coursePublishInfo", coursePublishVo);
    }

    @ApiOperation(value = "修改课程的发布状态")
    @PutMapping("updateCourseStatus/{courseId}")
    public R updateCourseStatus(@PathVariable("courseId") String courseId) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return R.ok();
    }

    @ApiOperation(value = "查询课程列表带分页查询")
    @GetMapping("getCourseList/{current}/{size}")
    public R getCourseList(@PathVariable("current") Long current, @PathVariable("size") Integer size, CourseInfoVo courseInfoVo) {
        Page<CourseInfoVo> page = new Page(current, size);
        Page<CourseInfoVo> courseInfoVoPage = courseService.getPageCourseInfoVo(page, courseInfoVo);
        return R.ok().data("page", courseInfoVoPage);
    }

    /**
     * 删除课程，直接删除课程下面的章节和小节以及课程描述
     * @param id
     * @return
     */
    @ApiOperation(value = "删除课程")
    @DeleteMapping("{id}")
    public R deleteCourseById(@PathVariable("id") String id) {
        courseService.deleteCourseCascade(id);
        return R.ok().message("删除课程成功");
    }

}
